/* =============================================================================
	ADD NEW GUEST EMAIL FIELD
============================================================================= */

var counter   = 2
var lastField = 2

// =============================================================================

function member_field() {
  if (counter < 6) {
    var currentID = $(":focus").attr("id")
    
    var allFilled = true
    
    console.log(counter)
    
    for (var i = 0; i <= counter; i ++)
      if ($("#" + i).val() == '') {
        allFilled = false
      }
    
    if (allFilled)
      add_member_field()
//    else
//      remove_member_field();
  }
}

// =============================================================================

function add_member_field() {
  var htmlToAdd = '<fieldset><input type="email" class="animated fadeInDown" id="' + lastField + '" name="email[' + lastField + ']" placeholder="Group Member\'s Email (Optional)" onkeypress="member_field()"></fieldset>'
  
  $(".add_group_member").append(htmlToAdd)
  
  lastField++
  counter++
}

// =============================================================================

function remove_member_field() {
  var currentID = $(":focus").attr("id")
  
  for (var i = 1; i < counter; i ++) {
    if (i != currentID) {
      if ($("#" + i).val() == '') {
        $("#" + i).removeClass("fadeInDown").addClass("fadeOutUp")
        
        setTimeout(function() {
          $("#" + i).remove()
        }, 5000)
        counter--
      }
    }
  }
}

// End of File